//package com.hey.amqpdemo.config;
//
//import com.hey.amqpdemo.listener.ConfirmCallBackListener;
//import com.rabbitmq.client.AMQP;
//import com.rabbitmq.client.Channel;
//import com.rabbitmq.client.impl.AMQImpl;
//import org.springframework.amqp.core.*;
//import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
//import org.springframework.amqp.rabbit.connection.Connection;
//import org.springframework.amqp.rabbit.connection.ConnectionFactory;
//import org.springframework.amqp.rabbit.core.RabbitAdmin;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//
///**
// * @ClassName RabbitConfiguration rabbitConfig内部定义
// * @Author hey
// * @Date 2019/3/25 9:51
// **/
//@Configuration
//public class RabbitConfiguration {
//
//    public static final String ORDER_QUEUE = "orderQueue";
//    public static final String GOODS_QUEUE = "goodsQueue";
//    public static final String USER_QUEUE = "userQueue";
//    public static final String PAY_QUEUE = "payQueue";
//
//    public static final String PAY_TOPIC_EXCHANGE = "payExchange";
//    public static final String ROUTING_KEY = "pay.*"; //路由键
//    public static final String ORDER_BINDING_BEAN_NAME = "orderBinding";
//    public static final String USER_BINDING_BEAN_NAME = "userBinding";
//    public static final String GOODS_BINDING_BEAN_NAME = "goodsBinding";
//    public static final String PAY_BINDING_BEAN_NAME = "payBinding";
//
//    @Autowired
//    ConfirmCallBackListener confirmCallBackListener;
//
//    @Bean
//    public ConnectionFactory connectionFactory(){
//        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory("localhost");//绑定host
//        cachingConnectionFactory.setPublisherConfirms(true);//确认
//        cachingConnectionFactory.setPublisherReturns(true);//返回
//        return cachingConnectionFactory;
//    }
//
//    /**
//     * rabbitAdmin用于声明队列，交换机，binding key
//     * @return
//     */
//    @Bean
//    public AmqpAdmin amqpAdmin(){
//        return new RabbitAdmin(connectionFactory());
//    }
//
//    /**
//     * rabbitTemplate用户mq的消息发送
//     * @return
//     */
//    @Bean
//    public RabbitTemplate rabbitTemplate(){
//        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
//        rabbitTemplate.setMandatory(true);//设置echange到queue失败的回调return为true
//        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());//设置序列化为json
//        rabbitTemplate.setConfirmCallback(confirmCallBackListener);
//        return rabbitTemplate;
//    }
//
//    @Bean
//    public Connection connection(){
//        Connection connection = connectionFactory().createConnection();
//        return connection;
//    }
//
//    @Bean(name = ORDER_QUEUE)
//    public Queue orderQueue(){
//        return new Queue(ORDER_QUEUE,true);
//    }
//
//    @Bean(name = USER_QUEUE)
//    public Queue userQueue(){
//        return new Queue(USER_QUEUE,true);
//    }
//
//    @Bean(name = GOODS_QUEUE)
//    public Queue goodsQueue(){
//        return new Queue(GOODS_QUEUE,true);
//    }
//
//    @Bean(name = PAY_QUEUE)
//    public Queue payQueue(){
//        return new Queue(PAY_QUEUE,true);
//    }
//
//    @Bean
//    public TopicExchange exchange(){
//        return new TopicExchange(PAY_TOPIC_EXCHANGE,true,false);
//    }
//
//    @Bean(name = ORDER_BINDING_BEAN_NAME)
//    public Binding orderBinding(){
//        return BindingBuilder.bind(orderQueue()).to(exchange()).with(ROUTING_KEY);
//    }
//
//    @Bean(name = GOODS_BINDING_BEAN_NAME)
//    public Binding goodsBinding(){
//        return BindingBuilder.bind(goodsQueue()).to(exchange()).with(ROUTING_KEY);
//    }
//
//    @Bean(name = USER_BINDING_BEAN_NAME)
//    public Binding userBinding(){
//        return BindingBuilder.bind(userQueue()).to(exchange()).with(ROUTING_KEY);
//    }
//
//    @Bean(name = PAY_BINDING_BEAN_NAME)
//    public Binding payBinding(){
//        return BindingBuilder.bind(payQueue()).to(exchange()).with(ROUTING_KEY);
//    }
//}
